package com.feishi.core.util.serialize.common;

import java.io.*;

/**
 * Created by caixq on 4/27 027.
 */
public class SerializeCommonUtil {

    public static <T> T readObject(byte[] bytes,Class<T> T) throws IOException, ClassNotFoundException {
        return readObject(new ByteArrayInputStream(bytes),T);
    }
    public static <T> T readObject(InputStream in, Class<T> T) throws IOException, ClassNotFoundException {
        ObjectInputStream input = new ObjectInputStream(in);
        T readObject = (T) input.readObject();
        input.close();
        return readObject;
    }

    public static <T> void writeObject(OutputStream stream, T t ) throws IOException{
        ObjectOutputStream out = new ObjectOutputStream(stream);
        out.writeObject( t);
        // DON'T out.close() when using factory method;
        out.flush();
    }
    public static <T> byte[] getBytes(T t ) throws IOException{
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream output = new ObjectOutputStream(out);
        output.writeObject( t);
        // DON'T out.close() when using factory method;
        output.flush();
        byte[] arr = out.toByteArray();
        out.close();
        return arr;
    }
}
